import pygame
import random
import easygui
from consts import *


class Base():
    def __init__(self, pygame_screen, postion, image_name):
        self.xn, self.yn = postion
        self.screen = pygame_screen
        self.image = image_name
        self.w, self.h = self.image.get_size()


class Background(Base):
    def __init__(self, pygame_screen, image_name, type):
        Base.__init__(self, pygame_screen, (0, 0), image_name)
        self.movedistance = 852 // 200
        self.type = type
        self.image = image_name
        self.screen = pygame_screen

    def display(self):
        if self.type == 'dynamic':
            self.yn += self.movedistance
            self.y2 = self.yn - self.h
            if self.yn >= 852:
                self.yn = 0
            self.screen.blit(self.image, (self.xn, self.y2))
        self.screen.blit(self.image, (self.xn, self.yn))


class Enemy2(pygame.sprite.Sprite):
    def __init__(self, enemy2_picture, enemy2_pos, hp):
        pygame.sprite.Sprite.__init__(self)
        self.image = enemy2_picture
        self.rect = self.image.get_rect()
        self.rect.topleft = enemy2_pos
        self.speed = 4

        # 爆炸动作索引
        self.down2_index = 0
        self.hit_count = 0
        self.max_hit_count = 6
        self.hit = False
        self.hp = hp

        self.gift = random.randint(0, 10)

    def update(self, kk2=0):
        self.rect.top += self.speed + kk2
        if self.rect.top > w:
            self.kill()

    def down(self):
        if self.hit:
            pass
        else:
            self.kill()


class Enemy(pygame.sprite.Sprite):
    def __init__(self, enemy_picture, enemy_pos):
        pygame.sprite.Sprite.__init__(self)
        self.enemy_pos = enemy_pos
        self.image = enemy_picture
        self.rect = self.image.get_rect()
        self.rect.topleft = self.enemy_pos
        self.speed = 6

        # 爆炸动作索引
        self.down_index = 0

    def update(self):
        self.rect.top += self.speed
        if self.rect.top > w:
            self.kill()


class Pa(pygame.sprite.Sprite):
    def __init__(self, pa_picture, pa_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pa_picture
        self.rect = self.image.get_rect()
        self.rect.topleft = pa_pos
        self.speed = 8

    def update(self):
        self.rect.top -= self.speed
        if self.rect.top < -self.rect.height:
            self.kill()


class Player(pygame.sprite.Sprite):
    def __init__(self, player_picture, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = player_picture
        self.pos = pos
        self.rect = self.image.get_rect()
        self.rect.topleft = pos
        self.spend = 6
        self.pas = pygame.sprite.Group()
        self.is_hit = False

    def move(self, bool):
        if bool:
            x, y = pygame.mouse.get_pos()
            x -= player_plane1.get_width() / 2
            y -= player_plane1.get_height() / 2
            del self.pos[(-len(self.pos)):]
            self.pos.extend([x, y])
        else:
            x1, y1 = pygame.mouse.get_pos()
            x1 -= player_plane2.get_width() / 2
            y1 -= player_plane2.get_height() / 2
            del self.pos[(-len(self.pos)):]
            self.pos.extend([x1, y1])
        self.rect = self.image.get_rect()
        self.rect.topleft = self.pos

    def shoot(self, pa_surface):
        self.pa = Pa(pa_surface, self.rect.midtop)
        self.pas.add(self.pa)


class Gift(pygame.sprite.Sprite):
    def __init__(self, picture, gift_pos):
        pygame.sprite.Sprite.__init__(self)
        self.enemy_pos = gift_pos
        self.image = picture
        self.rect = self.image.get_rect()
        self.rect.topleft = self.enemy_pos
        self.speed = 2

    def update(self):
        self.rect.top += self.speed
        if self.rect.top > w:
            self.kill()


class Max_pa(pygame.sprite.Sprite):
    def __init__(self, pic, max_pos):
        pygame.sprite.Sprite.__init__(self)
        self.max_pos = max_pos
        self.image = pic
        self.rect = self.image.get_rect()
        self.rect.topleft = self.max_pos
        self.speed = 6

    def update(self):
        self.rect.top -= self.speed
        if self.rect.top < 0:
            self.kill()
